package com.ks.member.service.impl;

import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.util.MD5Util;
import com.ks.core.util.StringUtil;
import com.ks.core.util.ToolUtil;
import com.ks.member.dao.MemberBaseMapper;
import com.ks.member.entity.MemberBase;
import com.ks.member.entity.MemberThirdAuthor;
import com.ks.member.dao.MemberThirdAuthorMapper;
import com.ks.member.service.IMemberThirdAuthorService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 第三方系统授权绑定信息 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-24
 */
@Service
public class MemberThirdAuthorServiceImpl extends ServiceImpl<MemberThirdAuthorMapper, MemberThirdAuthor> implements IMemberThirdAuthorService {

    @Resource
    private MemberThirdAuthorMapper memberThirdAuthorMapper;
    @Resource
    private MemberBaseMapper memberBaseMapper;

    @Override
    public Map<String, Object> addMemberThirdAuthor(String openId, String unionId, Integer tenantId, String mobile) {
        Map<String, Object> map = new HashMap<>();
        MemberThirdAuthor memberThirdInfo = getMemberThirdInfo(openId,tenantId);
        if (ToolUtil.isNotEmpty(memberThirdInfo)) {
            MemberBase memberBase = memberBaseMapper.selectById(memberThirdInfo.getIUserId());
            memberBase.setAuthToken(getAuthToken(memberBase));
            if (ToolUtil.isNotEmpty(memberBase)) {
                map.put("memberBase", memberBase);
            }
        } else {
            MemberBase base = new MemberBase();
            base.setITel(mobile);
            base.setTenantID(tenantId);
            MemberBase base1 = memberBaseMapper.selectOne(base);
            if (ToolUtil.isNotEmpty(base1)) {
                MemberThirdAuthor thirdAuthor = getThirdAuthor(base1.getTenantID(), base1.getId());
                if (ToolUtil.isNotEmpty(thirdAuthor)) {
                    base1.setAuthToken(getAuthToken(base1));
                    map.put("memberBase", base1);
                } else {
                    MemberThirdAuthor author = new MemberThirdAuthor();
                    author.setIUnionID(unionId);
                    author.setIOpenID(openId);
                    author.setIUserId(base1.getId());
                    author.setTenantID(tenantId);
                    author.setITag("授权微信小程序");
                    memberThirdAuthorMapper.insert(author);
                    base1.setAuthToken(getAuthToken(base1));
                    map.put("memberBase", base1);
                }
            } else {
                String Salt = StringUtil.getStringRandom(8);
                MemberBase memberBase = new MemberBase();
                memberBase.setIPassWord(MD5Util.getMD5(MD5Util.getMD5("123456") + Salt));
                memberBase.setCSalt(Salt);
                memberBase.setITel(mobile);
                memberBase.setTenantID(tenantId);
                memberBase.setOrgID(0);
                memberBase.setOrgSubID(0);
                memberBaseMapper.insert(memberBase);
                memberBase.setAuthToken(getAuthToken(memberBase));
                MemberThirdAuthor author = new MemberThirdAuthor();
                author.setIUnionID(unionId);
                author.setTenantID(tenantId);
                author.setIOpenID(openId);
                author.setIUserId(memberBase.getId());
                author.setITag("授权微信小程序");
                memberThirdAuthorMapper.insert(author);
                map.put("memberBase", memberBase);
            }
        }
        return map;
    }

    @Override
    public String getAuthToken(MemberBase memberBase) {
        String auth = memberBase.getTenantID() + ":" + memberBase.getOrgID() + ":" + memberBase.getOrgSubID() + ":" + memberBase.getId() + ":" + new Date().getTime();
        String md5 = MD5Util.getMD5(auth + ":" + memberBase.getCSalt());
        String token = auth + ":" + md5;
        return token;
    }

    public MemberThirdAuthor getMemberThirdInfo(String openId,Integer tenantId) {
        MemberThirdAuthor memberThirdAuthor = new MemberThirdAuthor();
        memberThirdAuthor.setIOpenID(openId);
        memberThirdAuthor.setTenantID(tenantId);
        MemberThirdAuthor thirdAuthor = memberThirdAuthorMapper.selectOne(memberThirdAuthor);
        return thirdAuthor;
    }


    public MemberThirdAuthor getThirdAuthor(int tenantId, int userId) {
        MemberThirdAuthor thirdAuthor = new MemberThirdAuthor();
        thirdAuthor.setTenantID(tenantId);
        thirdAuthor.setIUserId(userId);
        thirdAuthor.setDelMark(Const.DEL_FLAG_NORMAL);
        MemberThirdAuthor memberThirdAuthor = memberThirdAuthorMapper.selectOne(thirdAuthor);
        return memberThirdAuthor;
    }
}
